﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SpaceMan.Physics;
using SpaceMan.Entities.Attachments;


using Microsoft.Xna.Framework.Input;

namespace SpaceMan.Entities
{
    
    public class C_Player : I_Entity
    {
        KeyboardState kbState, lkbState;
        C_ATT_StandardSpeaker EngineNoise;
        C_ATT_StandardSpeaker Brake;
        I_AttachmentWeapon weapon1, weapon2;
        C_EFF_LightFlash flash;

        float engineWindUp;
        public Vector2 TargetPosition;
        public C_Player()
        {
        }

        public override void Initialize(Vector2 Position)
        {
            
            Health = 1000;
            baseAttachment = new IC_ATT_Attachment();
            Dictionary<string, Vector2> attachmentPoints = new Dictionary<string, Vector2>();
            attachmentPoints.Add("Sound1", Vector2.Zero);
            attachmentPoints.Add("Sound2", Vector2.Zero);
            attachmentPoints.Add("Sound3", Vector2.Zero);

            attachmentPoints.Add("Weapon1", new Vector2(66,141));
            attachmentPoints.Add("Weapon2", new Vector2(157, 136));

            attachmentPoints.Add("FlashPoint1", new Vector2(157, 136));
            attachmentPoints.Add("FlashPoint2", new Vector2(66, 141));


            baseAttachment.BaseInitialize(attachmentPoints);
            EngineNoise = new C_ATT_StandardSpeaker("Vehicles//HeavyAmbience", true);
            Brake = new C_ATT_StandardSpeaker("Vehicles//Brake", false);
            weapon1 = new C_ATT_LaserBlaster(DamageAffinity.Allied);
            weapon2 = new C_ATT_LaserBlaster(DamageAffinity.Allied);
            flash = new C_EFF_LightFlash(Vector2.Zero, 0.2f, 0.0005f, 250, true);
            baseAttachment.AddAttachment("FlashPoint1", flash);
            baseAttachment.AddAttachment("Sound1", EngineNoise);
            baseAttachment.AddAttachment("Sound2", Brake);
            baseAttachment.AddAttachment("Weapon1", weapon1);
            baseAttachment.AddAttachment("Weapon2", weapon2);



            EngineNoise.PlaySound();

            base.Position = Position;
            base.DrawOperation = S_Global.getDraw("QUXALPHA");
            
            List<PHY_Collider> colliderList = new List<PHY_Collider>();
            //
            PHY_Collider Collider = new PHY_Collider();
            Collider.Initialize(new Vector2(8, 222), 74, 152, false);
            Collider.MakeDamageable(DamageAffinity.Allied, 1.10f, Hurt);
            colliderList.Add(Collider);
            //
            Collider = new PHY_Collider();
            Collider.Initialize(new Vector2(146, 222), 74, 152, false);
            Collider.MakeDamageable(DamageAffinity.Allied, 1.10f, Hurt);
            colliderList.Add(Collider);
            //
            Collider = new PHY_Collider();
            Collider.Initialize(new Vector2(78, 26), 73, 347, false);
            Collider.MakeDamageable(DamageAffinity.Allied, 0.80f, Hurt);
            colliderList.Add(Collider);
            //
            rigid = new PHY_Base(Position, 40, 0.95f,false, colliderList, null, this.GetType().Name);
            rigid.currentState.V = Vector2.Zero;
        }

        public override void Update()
        {
            
            TargetPosition = new Vector2(rigid.BoundingBox.Center.X, rigid.BoundingBox.Center.Y);
            //rigid.Confine(new Vector2(S_Global.CurrrentlyVisibleWorld().X, S_Global.CurrrentlyVisibleWorld().Y), S_Global.CurrrentlyVisibleWorld().Width, S_Global.CurrrentlyVisibleWorld().Height);
            getInput();
            Position = rigid.currentState.Pos;
            baseAttachment.Update(Position, rigid.currentState.V,0f);
            S_Global.GlobalListener.Position = new Vector3(rigid.BoundingBox.Center.X, rigid.BoundingBox.Center.Y, 0);
            S_Global.GlobalListener.Velocity = new Vector3(rigid.currentState.V, 0);
            engineWindUp += (Math.Abs(rigid.TotalSpeed.Y) - engineWindUp) * 0.025f;        
            EngineNoise.SoundUpdate(0.5f, (Math.Abs(engineWindUp) * 0.0004f) - 0.45f);
            base.DrawOperation.Update(base.Position, 0f, null, null, true, null);
            DrawOperation.UpdateGLow(new Color(0, 150, 255, 128));
            if (Health <= 0)
            {
                MarkedForDeletion = true;
            }
        }

        public void Hurt(int Damage)
        {
            base.Health -= Damage;
        }

        void getInput()
        {
            kbState = Keyboard.GetState();

            if (kbState.IsKeyDown(Keys.W) && kbState.IsKeyUp(Keys.X))
            {
                rigid.Addforce(new Vector2(0,-800));
                engineWindUp += 11;
            }

            if (kbState.IsKeyDown(Keys.A))
            {
                rigid.Addforce(new Vector2(-800, 0));
            }

            if (kbState.IsKeyDown(Keys.S) && kbState.IsKeyUp(Keys.X))
            {
                rigid.Addforce(new Vector2(0, 800));
                engineWindUp -= 7;
            }

            if (kbState.IsKeyDown(Keys.D))
            {
                rigid.Addforce(new Vector2(800, 0));
            }

            if (kbState.IsKeyDown(Keys.X))
            {
                rigid.currentState.V *= 0.90f;
                engineWindUp *= 0.85f;
            }

            if (kbState.IsKeyDown(Keys.V))
            {
                flash.UserInput1();
            }

            if (kbState.IsKeyDown(Keys.Space) && lkbState.IsKeyUp(Keys.Space))
            {

            }

            if(kbState.IsKeyDown(Keys.Space))
            {
                 if (weapon1 != null)
                {
                    weapon1.Fire();
                }

                if (weapon2 != null)
                {
                    weapon2.Fire();
                }
            }

            if (kbState.IsKeyUp(Keys.Space) && lkbState.IsKeyDown(Keys.Space))
            {

         
            }

            if (kbState.IsKeyDown(Keys.X) && lkbState.IsKeyUp(Keys.X))
            {
                Brake.PlaySound();
            }
            else if (lkbState.IsKeyDown(Keys.X) && kbState.IsKeyUp(Keys.X))
            {
                Brake.StopSound();
            }


            lkbState = kbState;
        }
    }
}
